package com.mutual.assistance.user.utils;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.Serializable;

/**
 * @author chenyuxin
 * @version 1.0
 * @description 各个服务获取用户信息
 * @date 2023/4/15 18:02
 */
@Slf4j
public class SecurityUtil {

    public static UserBO getUser() {
        try {
            Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (principalObj instanceof String) {
                //取出用户身份信息
                String principal = principalObj.toString();
                //将json转成对象
                return JSON.parseObject(principal, UserBO.class);
            }
        } catch (Exception e) {
            log.error("获取当前登录用户身份出错:{}", e.getMessage());
            e.printStackTrace();
        }

        return null;
    }


    @Data
    public static class UserBO implements Serializable {

        private static final long serialVersionUID = 1L;

        private Long id;

        /**
         * 用户名
         */
        private String username;

        /**
         * 用户邮箱
         */
        private String email;

        /**
         * qq
         */
        private String qq;

        /**
         * 用户电话
         */
        private String cellphone;

        /**
         * 用户积分
         */
        private Integer credit;

        /**
         * 用户星级
         */
        private Integer star;

        /**
         * 用户状态
         */
        private String status;
    }
}
